<?php session_start();
if (!isset($_SESSION['userId'])) {
	header("location:index.php");
}
include ("../class/config.php");
include ("../class/cls_category.php");
include ('../plugins/tinymce/editor.php');
$allow_ext = array('jpg', 'JPG', 'jpeg', 'JPEG', 'png', 'PNG');
if (isset($_POST['add'])) {
	$cat_name = $_POST['category_name'];
	$cat_details = $_POST['category_details'];
	$date = date("Y-m-d H:i:s");
	$cat_image = $_FILES['category_image']['name'];
	$ext_array = explode(".", $cat_image);
	$ext = array_pop($ext_array);
	if (in_array($ext, $allow_ext)) 
	{
		move_uploaded_file($_FILES['category_image']['tmp_name'], '../uploads/country/' . $_FILES['category_image']['name']);
		//move files uploaded form browser option
		$objCategory -> addCategory($cat_name, $cat_details, $date, $cat_image);
		$success = " <span class='successmsg'>Process completed.</span>";
	} else {
		$success = "<span class='errormsg'>processed failed !! Check the informaton below</span>";
	}
}
if (isset($_GET['deleteId'])) {
	$catId = $_GET['deleteId'];
	$objCategory -> deleteCategory($catId);
}
if (isset($_GET['editId'])) {
	$catid = $_GET['editId'];
	if (isset($_POST['edit'])) {
		$catName = $_POST['category_name'];
		$cat_details = $_POST['category_details'];
		$catDate = date("Y-m-d h:i:s");
		$cat_image = $_FILES['category_image']['name'];
		$ext_array = explode(".", $cat_image);
		$ext = array_pop($ext_array);
		if (in_array($ext, $allow_ext)) {
			move_uploaded_file($_FILES['category_image']['tmp_name'], '../uploads/country/' . $_FILES['category_image']['name']);
			//move files uploaded form browser option
			$objCategory -> editCategory($catid, $catName, $cat_details, $catDate, $cat_image);
			$success = " <span class='successmsg'>Process completed.</span>";
	} else {
		$success = "<span class='errormsg'>processed failed !! Check the informaton below</span>";
	}
	
	}
}
?>
<script>
	var oEdit2 = new InnovaEditor("oEdit2");

		<?php	if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE")) { ?>
			oEdit2.cmdAssetManager = "modalDialogShow('../../../plugins/editor/assetmanager/assetmanager.php',800,800)";
			//Use "relative to root" path
		<?php } else { ?>
			oEdit2.cmdAssetManager = "modalDialogShow('../../../editor/assetmanager/assetmanager.php',800,800)";
			//Use "relative to root" path
		<?php } ?>
			oEdit2.btnFlash = true;
			//Show 'Insert Flash' button
			oEdit2.btnMedia = true;
			//Show 'Insert Media' button
			oEdit2.btnStyles = true;
			// show the css box
			oEdit2.height = 400;
			oEdit2.width = 500;
			oEdit2.css = './editor/assetmanager/style.css';

			oEdit2.RENDER(document.getElementById("idTemporary2").innerHTML); 
</script>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Category</title>
<script type="text/javascript" src="../lib/frmValidation.js"></script>
<link rel="stylesheet" href="../css/layout.css" />
</head>
<body>

	<div class="mainWrapper">
     <?php
		include ("../includes/adminHeader.php");
	?>
    
    
    <div class="bodyContent">
    <h1>Category</h1>
    <div class="innerBody">
    <?php if(!isset($_GET['action']))
	{?>
    <strong>Category Listing</strong>  | <a href="?action=add">Add Category</a>
     
	
      <table width="100%" border="0" cellpadding="1" cellspacing="1" class="table">
        <tr>
          <th>Sn</th>
          <th>Category Added Date</th>
          <th>Image</th>
          <th>Category Name</th>
          <th>Details</th>
          <th>Action</th>
        </tr>
		<?php $resCategory = $objCategory->selectCategory();
		$i=1;
		
		while($recCategory=mysqli_fetch_array($resCategory))
		{
		?>
        <tr>
          <td><?php echo $i++; ?></td>
          <td><?php echo $recCategory['cat_added_date']; ?></td>
          <td><img src="../uploads/country/<?php echo $recCategory['cat_image']; ?>"/></td>
          <td><?php echo $recCategory['cat_name']; ?></td>
          <td><?php echo $recCategory['cat_details']; ?></td>
		  
          <td><a href="?action=edit&editId=<?php echo $recCategory['cat_id']; ?>">Edit</a> | <a href="?deleteId=<?php echo $recCategory['cat_id']; ?>" onclick="javascript:return confirm('Are You Sure You Want to Delete this Record?')">Delete</a></td>
        </tr>
        <?php } /// end of while($recCategory=mysqli_fetch_array($resCategory)) ?>
       </table>
       
     <?php }
		else if(isset($_GET['action']) && $_GET['action']=='edit')
		{
		$catId=$_GET['editId'];
		$res=$objCategory->getCategoryById($catId);
		$rec=mysqli_fetch_array($res);
		//	 echo "<pre>";
		//                 print_r ($rec);
		//         echo "<pre/>";
		// exit;
	 ?>
      <strong>Edit Category</strong>
      <form id="categoryEdit" name="categoryEdit" method="post" action="" enctype="multipart/form-data"
      		 onsubmit="return editCat()">
        <table width="100%" border="0" class="table" cellpadding="1" cellspacing="1">
          <tr>
            <td width="30%">Category Name *</td>
            <td><input type="text" name="category_name" id="category_name" class="textBox" 
            	value="<?php echo $rec['cat_name']; ?>" />
            	<?php if(isset($_POST['category_name']) &&$_POST['category_name']==""){
            		echo "<span class='errormsg'>empty categoty name !</span>";
            	}?></td>
          </tr>
          <tr>
            <td width="30%">Category Details *</td>
            <td><textarea rows="5" cols="50" name="category_details" id="category_details" class="textBox" >
            	<?php echo($rec['cat_details']); ?></textarea>
            	<?php if(isset($_POST['category_details']) &&$_POST['category_details']==""){
            		echo "<span class='errormsg'>empty categoty details !</span>";
            	}?> </td>            	
          </tr>
          <tr>
          	<td width="30%">Image </td>
          	<td><input type="file" name="category_image" id="category_image"  />
          	<?php if(isset($_FILES['category_image']['name']) &&$_FILES['category_image']['name']==""){
            		echo "<span class='errormsg'>no file chosen !</span>";
            	}?></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="edit" id="edit" value="Edit" />
            	<input type="button" name="cancel" value="cancel" onclick="javascript:location.href='category.php'" /></td>
          </tr>
          <tr>
            <td>Note : * Fields are mandatory.</td>
            <td>&nbsp;</td>
          </tr>
        </table>
      </form>
 <?php }
		else if(isset($_GET['action']) && $_GET['action']=='add')
		{
	 ?>
      <strong>Add Category</strong>
      <?php
		if (isset($success)) {
			echo $success;
		}
 ?>
      <form id="addCategory" name="addCategory" method="post" action="" enctype="multipart/form-data" onsubmit="return addCat()">
        <table width="100%" border="0" class="table" cellpadding="1" cellspacing="1">
          <tr>
            <td width="30%">Category Name *</td>
            <td><input type="text" name="category_name" id="category_name" class="textBox" />
            	<?php if(isset($_POST['category_name']) &&$_POST['category_name']==""){
            		echo "<span class='errormsg'>empty categoty name !</span>";
            	}?>
            </td>
          </tr>
          <tr>
            <td width="30%">Category Details *</td>
            <td><textarea rows="5" cols="50" name="category_details" id="category_details" class="textBox" /></textarea>
            	<?php if(isset($_POST['category_details']) &&$_POST['category_details']==""){
            		echo "<span class='errormsg'>empty categoty detail !</span>";
            	}?></td>
          </tr>
          <tr>
          	<td width="30%">Image </td>
          	<td><input type="file" name="category_image" id="category_image"  />
          		<?php if(isset($_FILES['category_image']['name']) &&$_FILES['category_image']['name']==""){
            		echo "<span class='errormsg'>no image chosen !</span>";
            	}
            	if((isset($_FILES['category_image']['name']) && in_array($ext, $allow_ext)=="FALSE")){
            		echo "<span class='errormsg'>invalide image formate !</span>";
            	} ?>
          	</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="add" id="add" value="Add"  />
            	<input type="button" name="cancel" value="cancel" onclick="javascript:location.href='category.php'" /></td>
          </tr>
          <tr>
            <td>Note : * Fields are mandatory.</td>
            <td>&nbsp;</td>
          </tr>
        </table>
      </form>
<?php } ?>
    </div>
    
    </div>
    
    
    <?php
		include ("../includes/adminFooter.php");
	?>
     
       </div>

</body>
</html>